package com.fourth.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Mr.ma
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class AlipayConfiguration {
    /**
     * 请求网关地址
     */
    @Value("${alipay.serverUrl}")
    public String serverUrl;

    /**
     * 商户appid
     */
    @Value("${alipay.appId}")
    public String appId;

    /**
     * 私钥 pkcs8格式的
     */
    @Value("${alipay.privateKey}")
    public String privateKey="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCLrq9h5V6KnodEaBOAXQ5YZyKHLe9sjvFFWOdQjIkxvtGNKgAdgqZcaK6GNTXzS5MfMh7HzGf0qmjRxhKPtsu3m6gew9DS1G8LCAHi2y+2tJFOz+1W4f8Nn6ow4l/DIzgi2yr1JD10TkxLg5rvPo7iKOqWkBsT/8QT/TQjFXC30A5/eGzXHCduy0dCTq3JgipEjFgGypP4v9NTquuZqizjYVVSzyNv9UZqPGuaIB8QEyFscBOeHtTJTdc2Qui8DCUlfU7O5Ws2LyUJ33Hfb1WAapqL5LNfMh+ysvRQx0r3hXv2Z1H+JKVfFgBlA9AkGL8LQOP9Pdg7wL5y2JKBM10dAgMBAAECggEAAds9e3iX7BG1TIRxlegms6gTPmau4ut3a2uGD3z+qcmWcWDYhYvh+CJwB1qXr/cWKnj5DcC6tPa60+blAkH0rSkFb/s8QEEQhL4DgGdXV/9IWxrKVbhFMoInueSYTzidm2D8TivAIVaVDv/dxEY5kennTD74DSj+CyH3WmwDhn3ZStFg03bMZQA9kwoMUz9V5iqDK0z8GAgngp8DVmjKCTiGKVKxjeRbXNnaviu8ncrt4qEgi3JfZpNORBa7JRIOJgnt7SzDLIsWkCZFZp13T714DRkLpv9KAFFk6z+bfeTJ6W/2q5Si6H+dSmJlreywJoF37woOqCE6SxwIywMYgQKBgQDfYJCR3g3TZyR8o1QvV1X8agNsyzrYcqMHdSM0NnQgSZ+/X8HTMpwHFRI4j0aYXoau6xfx0YvYHCTDwbXXZMSIqWUNcH05ZNuma82PHbAVr4aTB3OsT6gzIpKdJnJ/ktGxB5PSNeYD4xgNmicjrZQUcgo/lSwELP5iNlpPAxafIQKBgQCgFQJpkWoSQHwBNPxxuISav6rqHKe3srbxQzBvjFJtlvJDZ3wnzRlkmUoPw+AOCGMZlCa0AAiZDfHd2MbmfNeQs1PuKQGEBidgRjzt9YPmZ7vTCKzECRcA9Wkpa2z4zYwr28IgZM+uYIEYtTQ92zkbsuVkekuGaLsulJJHTMRqfQKBgCKtXQQoEsFk27c1q9YFmuWql4mGK0Z61XBM8QAkC/cPNuuAtBhTXQGEdeWt6GmzEvmIqZjyzODpzZNOOSqNDC2jDnr9oZMNY0P8rFaxZyqWqy+cEosHerclgNlApLnFUyRGZt5VW7XyxUMnUTgqtDhQU+L3B3RGf99DTNXDRvnBAoGAMFoIL4DHjBnEGs05TtQP4XnxEuNy2S52yWqk75xvrUBBjpBsVM1DVTpjv11HiJWJ8UjpY8b60df/bSWz9hxRNBimSjJCjfEZ5Z9xeeKFY4uDlNty64dfVl/J0sPq1z+mjfzDpussO5xUgfUBgYl8t1wSo4W42DE8EHa5tCWA0jUCgYEA30UjY3NsXAUdn22xqWCjzt3L7jfI84Cb72QqV52b2T18w3GCem7+ynSoB5rPP1UYPTU72JJGVUeY6La5u1H9C/krfXGit+0PcQXxgKekb9IXAIK5sQB1LKBOh649QlNzmZE5IlgwAPtljqY9up9V+UDn6wSJDtpVna5iCHigJzY=";

    /**
     * 返回格式
     */
    @Value("${alipay.format}")
    public String format;

    /**
     * 编码
     */
    @Value("${alipay.charset}")
    public String charset;

    /**
     * 支付宝公钥
     */
    @Value("${alipay.alipayPublicKey}")
    public String alipayPublicKey;

    /**
     * RSA2
     */
    @Value("${alipay.signType}")
    public String signType ;

    /**
     * 服务器异步通知页面路径,需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     * notify_url文件主要是写入业务处理逻辑代码
     */
    @Value("${alipay.notify_url}")
    public String notify_url;

    /**
     * 支付成功后的回调地址，可以在方法中修改支付状态等信息
     * 该页面仅做页面展示，获取支付宝GET过来反馈信息
     * 支付宝服务器同步通知页面
     * 返回支付的相关信息
     * 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，
     * 必须外网可以正常访问 商户可以自定义同步跳转地址
     */
   @Value("${alipay.return_url}")
    public String return_url;

    /**
     * 日志记录目录定义在 logFile 中
     */
    @Value("${alipay.log_path}")
    public String log_path;


    public void logResult(String sWord) {
        //写入路径
        File file = new File(log_path);
        //判断路径是否存在，不存在就创建
        if (!file.exists()) {
            System.out.println("目标文件所在路径不存在，准备创建。。。");
            file.mkdirs();
            if (!file.mkdirs()) {
                System.out.println("创建成功");
            } else {
                System.out.println("创建失败，请重试");
            }
        } else {
            System.out.println("文件路径已存在");
        }

        FileWriter writer = null;
        try {
            writer = new FileWriter(log_path + "alipay_log" + System.currentTimeMillis() + ".txt");
            writer.write(sWord);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}